import { fileUpload, filesUpload, fileDelete, fileListDelete, fileDownload } from '@/api/upload'
import { getToken } from '@/utils/auth'

// 附件上传
export function fileUploadFunc(formdata) {
  return new Promise((resolve, reject) => {
    fileUpload(formdata).then((res) => {
      if (res.code === 200) {
        resolve(res.data)
      } else {
        reject(res.msg)
      }
    })
  })
}
// 附件上传---多个上传
export function fileListUploadFunc(formdata) {
  return new Promise((resolve, reject) => {
    filesUpload(formdata).then((res) => {
      if (res.code === 200) {
        resolve(res.data)
      } else {
        reject(res.msg)
      }
    })
  })
}
// 附件删除
export function fileDeleteFunc(data) {
  return new Promise((resolve, reject) => {
    fileDelete(data).then((res) => {
      if (res.code === 200) {
        resolve(res)
      } else {
        reject(res)
      }
    })
  })
}

// 附件批量删
export function fileListDeleteFunc(data) {
  return new Promise((resolve, reject) => {
    fileListDelete(data).then((res) => {
      if (res.code === 200) {
        resolve(res)
      } else {
        reject(res)
      }
    })
  })
}

// 附件编辑
export function fileEditFun(fileItem) {
  let pag = 'edit'
  // switch (fileItem.suffix) {
  //   case '.doc':
  //     pag = 'editWord'
  //     break
  //   case '.xls':
  //     pag = 'editExcel'
  //     break
  //   case '.xlsx':
  //     pag = 'editExcel'
  //     break
  //   case '.pdf':
  //     pag = 'readPdf'
  //     break
  // }
  console.log('编辑'+ getToken())
  POBrowser.headerToken = 'Authorization:' + getToken()
  POBrowser.openWindowModeless(`${process.env.VUE_APP_FILE_API}/${pag}?id=${fileItem.id}`, 'width=1200px;height=800px;')
}

// 附件预览
export function filePreviewFun(fileItem) {
  let pag = 'read'
  // switch (fileItem.suffix) {
  //   case '.jpg':
  //     pag = 'openImage'
  //     break
  //   case '.jpeg':
  //     pag = 'openImage'
  //     break
  //   case '.png':
  //     pag = 'openImage'
  //     break
  //   case '.doc':
  //     pag = 'readWord'
  //     break
  //   case '.xls':
  //     pag = 'readExcel'
  //     break
  //   case '.xlsx':
  //     pag = 'readExcel'
  //     break
  //   case '.pdf':
  //     pag = 'readPdf'
  //     break
  // }
  console.log('预览' + getToken())
  POBrowser.headerToken = 'Authorization:' + getToken()
  console.log('POBrowser : ' , POBrowser)
  POBrowser.openWindowModeless(`${process.env.VUE_APP_FILE_API}/${pag}?id=${fileItem.id}`, 'width=1200px;height=800px;')
}

// 附件下载
export function downloadFun(file) {
  const types = blobType(file)
  return new Promise((resolve, reject) => {
    fileDownload({ id: file.id }).then(res => {
      const blob = new Blob([res], { type: types })
      const url = window.URL.createObjectURL(blob)
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', file.old_name + file.suffix || '附件' + file.suffix)
      document.body.appendChild(link)
      link.click()
      resolve()
    }).catch(() => {
      reject()
    })
  })
}

// 附件类型
export function blobType(file) {
  let types = ''
  let suffix = ""
  if (file.suffix){
    suffix = file.suffix
  }else{
    let arr = file.name.split('.')
    if (arr.length){
      suffix = '.' + arr[arr.length-1]
    }
  }
  switch (suffix) {
    case '.jpg':
      types = 'image/jpeg'
      break
    case '.jpeg':
      types = 'image/jpeg'
      break
    case '.png':
      types = 'image/png'
      break
    case '.gif':
      types = 'image/gif'
      break
    case '.doc':
      types = 'application/msword'
      break
    case '.docx':
      types = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
      break
    case '.xls':
      types = 'application/vnd.ms-excel'
      break
    case '.xlsx':
      types = 'application/vnd.ms-excel'
      break
    case '.pdf':
      types = 'application/pdf'
      break
    case '.ppt':
      types = 'application/vnd.ms-powerpoint'
      break
    case '.pptx':
      types = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
      break
    case '.txt':
      types = 'text/plain'
      break
    case '.zip':
      types = 'application/zip'
      break
    case '.rar':
      types = 'application/x-rar-compressed'
      break
    case '.mp3':
      types = 'audio/mpeg'
      break
    case '.wav':
      types = 'audio/wav'
      break
    case '.wmv':
      types = 'video/x-ms-wmv'
      break
  }
  return types
}

// 附件类型
export function fileTypeTransform(file) {
  let types = ''
  if (file.type){
    switch (file.type) {
      case 'image/jpg':
        types = '.jpg'
        break
      case 'image/jpeg':
        types = '.jpeg'
        break
      case 'image/png':
        types = '.png'
        break
      case 'image/gif':
        types = '.gif'
        break
      case 'application/msword':
        types = '.doc'
        break
      case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
        types = '.docx'
        break
      case 'application/vnd.ms-excel':
        types = '.xls'
        break
      case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
        types = '.xlsx'
        break
      case 'application/pdf':
        types = '.pdf'
        break
      case 'application/vnd.ms-powerpoint':
        types = '.ppt'
        break
      case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
        types = '.pptx'
        break
      case 'text/plain':
        types = '.txt'
        break
      case 'application/x-zip-compressed':
        types = '.zip'
        break
      case 'application/x-rar-compressed':
        types = '.rar'
        break
      case 'audio/mpeg':
        types = '.mp3'
        break
      case 'video/wav':
        types = '.wav'
        break
      case 'video/x-ms-wmv':
        types = '.wmv'
        break
    }
  }else{
    let arr = file.name.split('.')
    if (arr.length){
      types = '.' + arr[arr.length-1]
    }
  }

  return types
}
